﻿using System;

namespace CustomerContactSystem.WebForms.Admin.Sources {
    public partial class Edit : BasePage {
        protected void Page_Load(object sender, EventArgs e) {
            if (!IsValidPageRequest(true, true, "id")) {
                return;
            }

            GetSource();
        }

        private void GetSource() {
            var sourceId = int.Parse(Request.QueryString["id"]);
            var source = EnquirySourceRepository.GetById(sourceId);

            if (source == null) {
                return;
            }

            SourceName.Text = source.SourceName;
        }

        protected void UpdateSourceClick(object sender, EventArgs e) {
            if (!IsValidPageRequest(true, false, "id")) {
                return;
            }

            var sourceId = int.Parse(Request.QueryString["id"]);
            var source = EnquirySourceRepository.GetById(sourceId);

            if (source == null) {
                UpdateSourceResultMessage.Text = "Source not found!";
                return;
            }

            source.SourceName = SourceName.Text;
            EnquirySourceRepository.Update(source);

            Response.Redirect("~/Admin/Sources/List.aspx");
        }
    }
}